package com.jk.goodsclassify.classify.controller;

import com.jk.goodsclassify.classify.entry.Library;
import com.jk.goodsclassify.classify.entry.Location;
import com.jk.goodsclassify.classify.utils.DistanceUtils;
import com.jk.goodsclassify.classify.utils.InputUtil;
import org.apdplat.word.WordSegmenter;
import org.apdplat.word.segmentation.WordRefiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@RestController
public class ClassifyController {
    private final static Logger logger = LoggerFactory.getLogger(ClassifyController.class);
    private static Library library;
    private static List<Location> locations;

    @RequestMapping(value = "/classify" ,method = RequestMethod.POST)
    public List<String> Classify(String param){
        List<String> result = new ArrayList<>();
        //解析参数
        String[] goodsNames = param.split("\\n");
        //分词
        for(String goodsName:goodsNames){
            String goodsWords = WordRefiner.refine(WordSegmenter.seg(goodsName)).toString();
            String[] words = goodsWords.substring(1,goodsWords.length()-1).split(",");
            //获取坐标
            Location location = new com.jk.goodsclassify.classify.entry.Location(library,words);
            //计算最短距离
            DistanceUtils distanceUtils = new DistanceUtils(library);
            String target = distanceUtils.geeMinDistancePoint(location,locations);
            result.add(target);
        }
        return result;
    }

    @PostConstruct
    public void autoInit() throws IOException {
        /*//动态获取资源文件地址
        String refinePath = this.getClass().getClassLoader().getResource("word_refine_local.txt").getPath().substring(1);
        String dicPath = this.getClass().getClassLoader().getResource("dic_local.txt").getPath().substring(1);
        String confTempPath = this.getClass().getClassLoader().getResource("word.local.temp.conf").getPath();
        String confPath = this.getClass().getClassLoader().getResource("word.local.conf").getPath();
        //动态修改配置文件
        DataInputStream in = new DataInputStream(new FileInputStream(new File(confTempPath)));
        BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
        String line;
        int count = 0;
        PrintStream ps = null;
        //PrintStream original = System.out;
        //ps = new PrintStream(confPath);
        System.setOut(ps);
        while ((line = br.readLine()) != null) {
            count++;
            if(count==17){
                System.out.println("dic.path=classpath:dic.txt,"+dicPath);
            }else if(count == 63){
                System.out.println("word.refine.path="+refinePath);
            }else {
                System.out.println(line);
            }
        }
        System.setOut(original);*/
        //获取训练资源,单例
        if(library == null){
            library = new InputUtil("library.txt").getLibrary();
        }
        if (locations == null){
            locations = new InputUtil("locations.txt").getLocations();
        }
        WordRefiner.refine(WordSegmenter.seg("电热杯迷你小型办公室养生电炖杯煮粥杯养生杯加热牛奶杯宿舍陶瓷")).toString();
        System.out.println("加载完成");
    }
}
